/**
 * Created with JetBrains WebStorm.
 * User: Administrator
 * Date: 14-6-17
 * Time: 下午3:50
 * To change this template use File | Settings | File Templates.
 */
define(function(require,exports,module){
    var VERSION = "1.0",
        jQuery = $ = require("jquery-1.8.2.min");

    var defaults = {
        closeAny:true,
        open:function(frame){},
        action:function(frame){}
    };

    function accordion(element,options){
        this.$elem = $(element);
        this.options = $.extend(true,{},defaults,options);
        this.create();
    }
    accordion.prototype = {
        create:function(){
            var elem = this.$elem;
            if(elem.data('closeany') != undefined){
                this.options.closeAny = elem.data('closeany');
            }
            this._frames = elem.children(".accordion-frame");
            this.init();
        },
        init:function(){
            var that = this;
            this._frames.each(function(){
                var frame = this,
                    a = $(this).children(".heading"),
                    content = $(this).children(".content");

                if($(a).hasClass("active") && !$(a).attr("disabled") && $(a).data("action")!='none'){
                    $(content).show();
                    $(a).removeClass('collapsed');
                }else{
                    $(a).addClass('collspsed');
                }

                a.on("click",function(e){
                    e.preventDefault();
                    if($(this).attr('disabled') || $(this).data('action') == 'none'){
                        return false;
                    }

                    if(that.options.closeAny){
                        that.closeFrames();
                    }
                    //todo 当这个a标签是一个连接地址的时候用ajax方法
                    if($(content).is(":hidden")){
                        $(content).slideDown();
                        $(this).removeClass("collapsed");
                        that.options.open(frame);
                    }else{
                        $(content).slideUp();
                        $(this).addClass("collapsed");
                    }
                    that.options.action(frame);
                })
            })
        },
        closeFrames:function(){
            this._frames.children(".content").slideUp()
                .parent().children(".heading").addClass("collapsed");
        },
        destroy:function(){

        }
    }
});